#include <QtGui>
// #include <qt.cpp>
#include "dlg9x9.h"
#include "epollserver.h"
#include "dlgmy.h"
#include "dlgmsg.h"
#ifdef __cplusplus
extern "C" {
#endif


#define BTN_COUNT (2)
#define YES_COUNT (1)
Dlg9x9::Dlg9x9(QWidget *parent)
	: QDialog(parent)
{
	m_ask = MB_WAIT;


	QPushButton *btns[BTN_COUNT];

	QGridLayout *layout = new QGridLayout;

	srand((unsigned int) time(NULL));

	char rand_metrix[BTN_COUNT] = {0};
	int k, j = 1;

	for (int i = 0; i < 1000; i++) {
		k = rand() % BTN_COUNT;
		if (rand_metrix[k] != 0) {
			continue;
		}

		rand_metrix[k] = j++;
		if (j > YES_COUNT) {
			break;
		}
	}
#if defined(YES_COUNT) == 1
	rand_metrix[0] = 1;
	rand_metrix[1] = 0;
#endif
	layout->setColumnStretch(1, 1);
	char *context[BTN_COUNT] = {
		"NO",
		"YES",
#if defined(BTN_COUNT) > 4
		"YES",
		"YES"
#endif
	};
	// printf("%s %s \n", context[0], context[1]);

	for (int i = 0; i < BTN_COUNT; i++) {
		// printf("%d \n", rand_metrix[i]);
		btns[i] =    new QPushButton(context[rand_metrix[i]], this);
		btns[i]->setObjectName(context[rand_metrix[i]]);

		layout->addWidget(btns[i], i % YES_COUNT, i / YES_COUNT);
		connect(btns[i], SIGNAL(clicked()), this, SLOT(doBtnClick()));
	}

	setWindowFlags(Qt::WindowStaysOnTopHint);
	// emit set_net();
	setGeometry(0, 40, -1, -1);
	connect(btns[0], SIGNAL(clicked()), this, SLOT(set_net()));
	connect(btns[0], SIGNAL(clicked()), this, SLOT(do_find()));
	connect(this, SIGNAL(sigShow9x9()), this, SLOT(doShow9x9()));
	connect(this, SIGNAL(sigHide9x9()), this, SLOT(doHide9x9()));

	setLayout(layout);

	setWindowTitle(("Standard Dialogs"));

	// setWindowFlags(Qt::SubWindow);
	Dialog1 *dialog = new Dialog1();
	dialog->setWindowFlags(Qt::SubWindow);
	// dialog->show();
	// dialog->exec();


}

void QDialog::reject ()
{
	hide();
}
// void Dlg9x9::keyPressEvent(QKeyEvent *event)
// {
	// printf("event->key() %x %x\n",event->key(), Qt::Key_Escape);
	// QDialog::keyPressEvent(event);
	// switch(event->key()) {
	// //进行界面退出，重写Esc键，否则重写reject()方法
	// // case Qt::Key_Escape:
	// // 	event->ignore();
	// // 	break;

	// default:
	// 	QDialog::keyPressEvent(event);
	// }
	// QDialog::keyPressEvent(event);
	// event->ignore();

// }

// void Dlg9x9::findNext(const QString &name)
// {
// 	printf("do find\n");
// }
void Dlg9x9::doBtnClick()
{
	QString buttonName = sender()->objectName();

	if(buttonName.compare("NO") == 0) {
		// printf("you click no\n");
		m_ask = MB_CANCEL;
		// printf("m_ask = MB_CANCEL\n");
	} else if(buttonName.compare("YES") == 0) {
		// printf("you click YES\n");
		m_ask = MB_OK;
		// printf("m_ask = MB_OK\n");
	}

	hide();
	// TODO 按下所有YES

	// 隐藏窗体
}
void Dlg9x9::set_net()
{

	// printf("jjjjjjjj\n");

}
void Dlg9x9::do_find()
{
	// printf("do find %s()\n", __FUNCTION__);
}


void Dlg9x9::hello()
{
	/*
	 * qobject_cast<*> dynamic_cast<*> 动态强制转换类型，但不建议用dynamic_cast
	 */
	QPushButton *btn = qobject_cast<QPushButton *>(sender());

	// if (buttonName.compare("aaa") == 0) {
	// printf("ggggg %s \n", qPrintable(buttonName));
	// }
	// btn->setEnabled(false);
	QString buttonName = sender()->objectName();

	if(buttonName.compare("NO") == 0) {
		// exit(3);
	} else if(buttonName.compare("YES") == 0) {
		// exit(0);
	}
	// printf("btn Name %s\n", qPrintable(buttonName));
	// buttonName.text();
}

void Dlg9x9::onShow9x9()
{
	// printf("function %s()\n", __FUNCTION__);
	emit sigShow9x9();
}
void Dlg9x9::doShow9x9()
{
	// printf("function %s()\n", __FUNCTION__);
	QString buttonName = sender()->objectName();

	// printf("sender Name %s\n", qPrintable(buttonName));
	QPushButton *button = findChild<QPushButton *>("YES");
	if (button ) {
		// printf("find\n");
		// button->setEnabled(0);
	}
	show();
	m_ask = MB_WAIT;
	// printf("m_ask = MB_WAIT\n");
	// Dlg9x9 *dialog = new Dlg9x9;
	// dialog->show();

}

void Dlg9x9::onHide9x9()
{
	// printf("function %s()\n", __FUNCTION__);
	emit sigHide9x9();
	// hide();

}

void Dlg9x9::doHide9x9()
{
	hide();
}


int Dlg9x9::getAsk()
{
	return m_ask;
}

extern Dlg9x9 *g_dialog;
extern DlgMsg *g_dlgmsg;
int do_c1(void *ptr, int argc, char **argv)
{
	// printf("\n\nhello 3333333333333333333333  argv[1] %s\n\n\n", argv[1]);
	if (argc < 3) {
		return -1;
	}


	if (strcmp(argv[1], "9x9") == 0) {
		if (strcmp(argv[2], "0") == 0) {
			g_dialog->onHide9x9();
		} else if (strcmp(argv[2], "1") == 0) {
			g_dialog->onShow9x9();

		}
	} else if (strcmp(argv[1], "msg") == 0) {
		if (strcmp(argv[2], "0") == 0) {
			g_dlgmsg->onHide();
		} else if (strcmp(argv[2], "1") == 0) {
			g_dlgmsg->onShow();

		}
	}


}


/*获取对话框按钮状态*/
int do_dlg_ask(void *ptr, int argc, char **argv)
// int do_dlg_ask(int argc, char **argv)
{
	struct ep_con_t *pnode = (struct ep_con_t *)ptr;
	int ask = g_dialog->getAsk();
	char strout[2];

	// printf("ask %d\n", ask);
	strout[1] = '\0';
	strout[0] = ask + '0';
	send(pnode->sockfd, strout, sizeof(strout), 0);
}


#ifdef __cplusplus
}
#endif
